package com.czn.service.impl;

import com.czn.dao.AppointmentDao;
import com.czn.dao.OptionDao;
import com.czn.dao.SeekDao;
import com.czn.entity.Appointment;
import com.czn.entity.Seek;
import com.czn.service.ISeekService;
import com.czn.utils.DrugsUtils;
import com.czn.utils.OptionUtils;
import com.czn.vo.ResponseResult;
import org.beetl.sql.core.db.KeyHolder;
import org.beetl.sql.core.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author ChenZeNan
 * @create 2021-04-29 17:27
 */
@Service
public class SeekServiceImpl implements ISeekService {
    @Autowired
    private OptionDao optionDao;
    @Autowired
    private SeekDao seekDao;
    @Autowired
    private AppointmentDao appointmentDao;

    /**
     * 保存就诊记录
     *
     * @param map
     * @return
     */
    @Override
    public ResponseResult save(Map map, HttpSession session) {
        //每个预约只能生成一条就诊记录
        Integer appointmentId = Integer.valueOf(String.valueOf(map.get("appointmentId")));
        Query<Seek> query = seekDao.createQuery();
        query.andEq("appointment_id", appointmentId);
        Seek seek = query.single();
        if (seek == null) {
            seek = new Seek();
            seek.setPatientId(Integer.valueOf(String.valueOf(map.get("patientId"))));
            seek.setDays(String.valueOf(map.get("days")));
            seek.setDescribes(String.valueOf(map.get("describes")));
            seek.setIllname(String.valueOf(map.get("illnessName")));
            seek.setOptions(OptionUtils.getOptionIds(map));
            seek.setAppointmentId(Integer.valueOf(String.valueOf(map.get("appointmentId"))));
            //根据检查项，计算出所需的费用
            seek.setPrice(optionDao.getTotalPrice(OptionUtils.getOptionIds(seek.getOptions())));
            KeyHolder keyHolder = seekDao.insertReturnKey(seek);

            //修改预约记录状态
            Appointment appointment = new Appointment();
            appointment.setId(Integer.valueOf(String.valueOf(map.get("appointmentId"))));
            appointment.setStatus("2");
            appointmentDao.upsertByTemplate(appointment);

            //将新记录的id存储到session中
            session.setAttribute("seek_" + map.get("patientId"), keyHolder.getInt());
            return new ResponseResult("300", "保存成功");
        } else {
            return new ResponseResult("301", "该记录已经就诊完成");
        }
    }

    /**
     * 开药
     *
     * @param map
     * @return
     */
    @Override
    public ResponseResult update(Map map, HttpSession session) {
        Seek seek = new Seek();
        Integer id = (Integer) session.getAttribute("seek_" + map.get("patientId"));
        seek.setId(id);
        seek.setDrugs(DrugsUtils.getDrugsInfo(map));
        seekDao.upsertByTemplate(seek);
        return new ResponseResult("500", "信息更新成功！");
    }

    /**
     * 获取Seek记录
     *
     * @param patientId
     * @param appointmentId
     * @param session
     * @return
     */
    @Override
    public Seek findSeekById(Integer patientId, Integer appointmentId, HttpSession session) {
        Seek seek = null;
        Integer seekId = (Integer) session.getAttribute("seek_" + patientId);
        if (seekId != null) {//如果当前回话中插入就诊记录，可以从session中获取就诊id
            seek = seekDao.single(seekId);
        } else { //反之，就诊记录已经存在，根据预约编号查询
            Query<Seek> query = seekDao.createQuery();
            query.andEq("appointment_id", appointmentId);
            seek = query.single();
        }
        return seek;
    }

}
